home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / • Extras • / SGI STL / multimap.h < prev    next >
C/C++ Source or Header  |  1997-05-28  |  9KB  |  243 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  *
  26.  * Copyright (c) 1997
  27.  * Moscow Center for SPARC Technology
  28.  *
  29.  * Permission to use, copy, modify, distribute and sell this software
  30.  * and its documentation for any purpose is hereby granted without fee,
  31.  * provided that the above copyright notice appear in all copies and
  32.  * that both that copyright notice and this permission notice appear
  33.  * in supporting documentation.  Moscow Center for SPARC Technology makes no
  34.  * representations about the suitability of this software for any
  35.  * purpose.  It is provided "as is" without express or implied warranty.
  36.  *
  37.  */
  38.  
  39. #ifndef __SGI_STL_MULTIMAP_H
  40. #define __SGI_STL_MULTIMAP_H
  41.  
  42. # ifndef __SGI_STL_TREE_H
  43. #  include <tree.h>
  44. # endif
  45.  
  46. __BEGIN_STL_NAMESPACE
  47. __BEGIN_STL_FULL_NAMESPACE
  48. #define multimap __WORKAROUND_RENAME(multimap)
  49.  
  50. template <class Key, class T, __DFL_TMPL_PARAM(Compare,less<Key>), 
  51.                               __DFL_TYPE_PARAM(Alloc,alloc) >
  52. class multimap {
  53.     typedef multimap<Key, T, Compare, Alloc> self;
  54. public:
  55. // typedefs:
  56.  
  57.   typedef Key key_type;
  58.   typedef T data_type;
  59.   typedef pair<const Key, T> value_type;
  60.   typedef Compare key_compare;
  61.  
  62.   class value_compare : public binary_function<value_type, value_type, bool> {
  63.     friend class multimap<Key, T, Compare, Alloc>;
  64.     protected:
  65.         Compare comp;
  66.         value_compare(Compare c) : comp(c) {}
  67.     public:
  68.         bool operator()(const value_type& x, const value_type& y) const {
  69.             return comp(x.first, y.first);
  70.         }
  71.   };
  72.  
  73. private:
  74.   typedef rb_tree<key_type, value_type, 
  75.                   select1st<value_type>, key_compare, Alloc> rep_type;
  76. public:
  77.   typedef typename rep_type::pointer pointer;
  78.   typedef typename rep_type::reference reference;
  79.   typedef typename rep_type::const_reference const_reference;
  80.   typedef typename rep_type::iterator iterator;
  81.   typedef typename rep_type::const_iterator const_iterator; 
  82.   typedef typename rep_type::reverse_iterator reverse_iterator;
  83.   typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
  84.   typedef typename rep_type::size_type size_type;
  85.   typedef typename rep_type::difference_type difference_type;
  86.  
  87. private:
  88.   rep_type t;  // red-black tree representing multimap
  89.  
  90. public:
  91. // allocation/deallocation
  92.   multimap() : t(Compare()) {}
  93.   explicit multimap(const Compare& comp) : t(comp) {}
  94.   multimap(const value_type* first, const value_type* last) : 
  95.       t(Compare()) {
  96.         t.insert_equal(first, last);
  97.   }
  98.   multimap(const value_type* first, const value_type* last, 
  99.       const Compare& comp) : t(comp) {
  100.         t.insert_equal(first, last);
  101.   }
  102.   multimap(const_iterator first, const_iterator last) : 
  103.       t(Compare()) {
  104.         t.insert_equal(first, last);
  105.   }
  106.   multimap(const_iterator first, const_iterator last, 
  107.       const Compare& comp) : t(comp) {
  108.         t.insert_equal(first, last);
  109.   }
  110.   multimap(const self& x) : t(x.t) { }
  111.   self&
  112.   operator=(const self& x) {
  113.     t = x.t;
  114.     return *this; 
  115.   }
  116.  
  117.   // accessors:
  118.  
  119.   key_compare key_comp() const { return t.key_comp(); }
  120.   value_compare value_comp() const { return value_compare(t.key_comp()); }
  121.   iterator begin() { return t.begin(); }
  122.   const_iterator begin() const { return t.begin(); }
  123.   iterator end() { return t.end(); }
  124.   const_iterator end() const { return t.end(); }
  125.   reverse_iterator rbegin() { return t.rbegin(); }
  126.   const_reverse_iterator rbegin() const { return t.rbegin(); }
  127.   reverse_iterator rend() { return t.rend(); }
  128.   const_reverse_iterator rend() const { return t.rend(); }
  129.   bool empty() const { return t.empty(); }
  130.   size_type size() const { return t.size(); }
  131.   size_type max_size() const { return t.max_size(); }
  132.   void swap(multimap<Key, T, Compare, Alloc>& x) { t.swap(x.t); }
  133.  
  134.   // insert/erase
  135.  
  136.   iterator insert(const value_type& x) { return t.insert_equal(x); }
  137.   iterator insert(iterator position, const value_type& x) {
  138.     return t.insert_equal(position, x);
  139.   }
  140.   void insert(const value_type* first, const value_type* last) {
  141.     t.insert_equal(first, last);
  142.   }
  143.   void insert(const_iterator first, const_iterator last) {
  144.     t.insert_equal(first, last);
  145.   }
  146.   void erase(iterator position) { t.erase(position); }
  147.   size_type erase(const key_type& x) { return t.erase(x); }
  148.   void erase(iterator first, iterator last) { t.erase(first, last); }
  149.   void clear() { t.clear(); }
  150.  
  151.   // multimap operations:
  152.  
  153.   iterator find(const key_type& x) { return t.find(x); }
  154.   const_iterator find(const key_type& x) const { return t.find(x); }
  155.   size_type count(const key_type& x) const { return t.count(x); }
  156.   iterator lower_bound(const key_type& x) {return t.lower_bound(x); }
  157.   const_iterator lower_bound(const key_type& x) const {
  158.     return t.lower_bound(x); 
  159.   }
  160.   iterator upper_bound(const key_type& x) {return t.upper_bound(x); }
  161.   const_iterator upper_bound(const key_type& x) const {
  162.     return t.upper_bound(x); 
  163.   }
  164.    pair<iterator,iterator> equal_range(const key_type& x) {
  165.     return t.equal_range(x);
  166.   }
  167.   pair<const_iterator,const_iterator> equal_range(const key_type& x) const {
  168.     return t.equal_range(x);
  169.   }
  170.   friend bool operator==(const self&, const self&);
  171.   friend bool operator<(const self&, const self&);
  172. };
  173.  
  174.  
  175. __END_STL_FULL_NAMESPACE
  176.  
  177. // do a cleanup
  178. #  undef multimap
  179. // provide a way to access full funclionality 
  180. #  define __multimap__  __FULL_NAME(multimap)
  181.  
  182. template <class Key, class T, class Compare, class Alloc>
  183. inline bool operator==(const __multimap__<Key, T, Compare, Alloc>& x, 
  184.                        const __multimap__<Key, T, Compare, Alloc>& y) {
  185.   return x.t == y.t;
  186. }
  187.  
  188. template <class Key, class T, class Compare, class Alloc>
  189. inline bool operator<(const __multimap__<Key, T, Compare, Alloc>& x, 
  190.                       const __multimap__<Key, T, Compare, Alloc>& y) {
  191.   return x.t < y.t;
  192. }
  193.  
  194. # if defined (__STL_CLASS_PARTIAL_SPECIALIZATION )
  195. template <class Key, class T, class Compare, class Alloc>
  196. inline void swap(__multimap__<Key, T, Compare, Alloc>& a,
  197.                  __multimap__<Key, T, Compare, Alloc>& b) { a.swap(b); }
  198. # endif
  199.  
  200. # ifndef __STL_DEFAULT_TYPE_PARAM
  201. // provide a "default" multimap adaptor
  202. template <class Key, class T, class Compare>
  203. class multimap : public __multimap__<Key, T, Compare, alloc>
  204. {
  205.     typedef multimap<Key, T, Compare> self;
  206. public:
  207.     typedef __multimap__<Key, T, Compare, alloc> super;
  208.     __CONTAINER_SUPER_TYPEDEFS
  209.     // copy & assignment from super
  210.     __IMPORT_SUPER_COPY_ASSIGNMENT(multimap)
  211.     multimap() : super(Compare()) {}
  212.     explicit multimap(const Compare& comp) : super(comp) {}
  213.     multimap(const typename super::value_type* first, const typename super::value_type* last) : 
  214.         super(first, last, Compare()) { }
  215.     multimap(const typename super::value_type* first, const typename super::value_type* last, 
  216.         const Compare& comp) : super(first, last, comp) { }
  217.     multimap(typename super::const_iterator first, typename super::const_iterator last) : 
  218.         super(first, last, Compare()) { }
  219.     multimap(typename super::const_iterator first, typename super::const_iterator last, 
  220.         const Compare& comp) : super(first, last, comp) { }
  221. };
  222.  
  223. #  if defined (__STL_BASE_MATCH_BUG)
  224. template <class Key, class T, class Compare>
  225. inline bool operator==(const multimap<Key, T, Compare>& x, 
  226.                        const multimap<Key, T, Compare>& y) {
  227.   typedef __multimap__<Key,T,Compare,alloc> super;
  228.   return (const super&)x == (const super&)y;
  229. }
  230.  
  231. template <class Key, class T, class Compare>
  232. inline bool operator<(const multimap<Key, T, Compare>& x, 
  233.                       const multimap<Key, T, Compare>& y) {
  234.   typedef __multimap__<Key,T,Compare,alloc> super;
  235.   return (const super&)x < (const super&)y;
  236. }
  237. #  endif
  238. # endif /*  __STL_DEFAULT_TEMPLATE_PARAM */
  239.  
  240. __END_STL_NAMESPACE
  241.  
  242. #endif
  243.